import { ElMessage } from "element-plus";

const messages: Record<number, string> = {
  400: "请求参数错误",
  401: "登录过期，请重新登录",
  403: "没有操作权限",
  404: "资源不存在",
  500: "服务器错误",
  503: "服务不可用",

  10002: "服务器错误",
  10003: "Token为空",
  10005: "管理员不存在",
  10006: "管理员被冻结",
  10019: "无权限"
};

export function handleResponseError(error: any) {
  const status = error.response?.status;
  let message = error.message;

  if (status) {
    message = messages[status] || `请求失败 (${status})`;
  }

  const showError = error.config?.customOptions?.error_message_show !== false;
  if (showError) {
    ElMessage.error(message);
  }

  return Promise.reject(error);
}

export function getCodeMessage(code: number): string {
  return messages[code] || `请求失败 (${code})`;
}
